package com.sinodata.ns.versions;

import jt.classic.system.ISystem;
import jt.classic.system.init.SystemMounting;
import jt.classic.system.utils.AnnexTool;
import jt.classic.system.wordbook.WordBookInitializtion;
import jt.classic.system.wordbook.WordBookUtil;
import jt.classic.system.wordbook.XWordbookMain;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import org.limp.basework.console.XSystemConfig;
import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.annex.AnnexItem;
import org.limp.mine.annex.AttFileItem;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MobileSoftAction extends DispatchAction {
    private String REALPATH = (String) XSystemConfig.config().getProperty(XSystemConfig.XSYSTEM_REALPATH);

    public ActionForward upmobilesoft(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        CommonBean4HSQ cbh = new CommonBean4HSQ(request);
        Map params = cbh.getResource();
        String ver = (String) params.get("ver");
        AnnexItem annexItem = (AnnexItem) params.get(AnnexItem.ANNEX);

        if (annexItem != null && !annexItem.isEmpty()) {
            AttFileItem item = annexItem.getAnnexFile("mobliesoft");

            if (item != null) {
                File file = new File(REALPATH +
                        "app/mobileSoft/south-water-to-north.apk");
                AnnexTool.store(file, item);

                XWordbookMain wbmain = XWordbookMain.getInstance();
                wbmain.insertWordbookItem("mobileVersion", "version", ver, "版本号");

                WordBookInitializtion workbook = new WordBookInitializtion();

                try {
                    workbook.load();
                } catch (Exception ex) {
                	request.setAttribute("result", "上传新版本客户端软件异常,请重试");
                    ISystem.catchException(request, this, ex);
                }
                
                request.setAttribute("result", "上传新版本客户端软件成功");
            } else {
                request.setAttribute("result", "请上传新版本客户端软件");
            }
        } else {
        	request.setAttribute("result", "请上传新版本客户端软件");
        }

        return mapping.findForward("result");
    }
}
